package com.rabbiter.pm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbiter.pm.domain.Car;
import com.rabbiter.pm.domain.User;
import com.rabbiter.pm.domain.vo.UserVo;
import com.rabbiter.pm.service.UserService;
import com.rabbiter.pm.utils.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  用户前端控制器
 * </p>
 * @author rabbiter
 * @since 2023-03-21
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     */
  @PostMapping("/login")
  public ResultJson<Object> login(@RequestBody User user){
      return ResultJson.success(userService.login(user));
  }
    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/getUser")
    public ResultJson<Object> getUser(){
        return ResultJson.success(userService.getCurrentUser());
    }
    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public ResultJson<Object> logout(){
        return ResultJson.success(userService.logout());
    }

    /**
     * 注册
     */
  @PostMapping("/register")
  public ResultJson<Object> register(@RequestBody User user){
      return ResultJson.success(userService.register(user));
  }

  @GetMapping("/checkUsername")
  public ResultJson<Boolean> checkUsername(String username){
      return ResultJson.success(userService.getOne(new QueryWrapper<User>().eq("username",username))==null);
  }

    /**
     * 新增
     */
  @PostMapping("/add")
  public ResultJson<Boolean> addUser(@RequestBody User user){
      return ResultJson.success(userService.add(user));
  }

    /**
     * 根据用户ID删除用户信息
     */
  @GetMapping("/del")
  public ResultJson<Object> delUser(Integer uid){
      return ResultJson.success(userService.delUser(uid));
  }

    /**
     * 模糊查询用户
     */
  @GetMapping("/detail")
  public ResultJson<Object> getUser(User user){
      return ResultJson.success(userService.getUsers(user));
  }

  @PostMapping("/getUsers")
  public ResultJson<Object> getUsersPage(@RequestBody UserVo userVo){
      return ResultJson.success(userService.getUsersPage(userVo));
  }

    /**
     * 更改用户信息
     */
  @PostMapping("/update")
  public ResultJson<Object> update(@RequestBody User user){
      return ResultJson.success(userService.updateUser(user));
  }

    /**
     * 用户更改个人信息
     */
  @PostMapping("/updateByUser")
  public ResultJson<Object> updateByUser(@RequestBody User user){
      return ResultJson.success(userService.updateByUser(user));
  }

    /**
     * 重置密码
     */
  @GetMapping("/release")
  public ResultJson<Object> updatePassword(Integer uid){
      return ResultJson.success(userService.release(uid));
  }


  @GetMapping("/getUserByUid")
  public ResultJson<User> gerUserByUid(Integer uid){
      ResultJson<User> success = ResultJson.success(userService.getById(uid));
      return success;
  }


  @PostMapping("/userPay")
  public ResultJson<Boolean> userPay(@RequestBody User user){
      return ResultJson.success(userService.userPay(user));
  }


  @GetMapping("/dataType")
  public ResultJson<Object> getUserResCo(String person){
      return ResultJson.success(userService.getUserDataType(person));
  }

    @GetMapping("/dataMoney")
    public ResultJson<Object> getUserMoney(String person){
        return ResultJson.success(userService.getUserDataMoney(person));
    }

    /**
     * 查询用户用车记录
     */
    @GetMapping("/{userId}/car")
    public ResultJson<Object> getCarbyUser(@PathVariable("userId") Integer userId){
        List<Car> cars = userService.getUsedVehiclesByUserId(userId);
        return ResultJson.success(cars);
    }
    /**
     * 通过车辆查询用户使用记录
     */
    @GetMapping("/{tid}/user")
    public ResultJson<Object> getUserByCar(@PathVariable("tid") Integer tid){
        List<User> user = userService.getUserByCar(tid);
        return ResultJson.success(user);
    }


}

